package com.interceptor;

import com.qf.entity.UserEmp;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ResourceBundle;

/**
 * 2019/11/25
 * 郭程鹏
 * gcp1909
 * 面向代码面向君,不负时光不负卿
 */

//登录拦截
public class AnonInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //记载资源文件
        ResourceBundle login = ResourceBundle.getBundle("login");

        //获取请求路径
        String requestURI = request.getRequestURI();

        System.out.println(requestURI);

        //判断请路径是否是匿名请求
        if (login.containsKey(requestURI)){
            return true;
        }

        //判断是否是用户已经登录进行公共请求和权限请求
        HttpSession session = request.getSession();

        UserEmp user = (UserEmp) session.getAttribute("user");

        if (user!=null){
            return true;
        }else {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print("<script>alert('请您先登录');location.href='/login.jsp'</script>");
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
